﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Reflection;
using System.IO;
using eTerm.NetSocket.PlugIn;

namespace eTerm.NetSocket
{
    #region 转发服务器
    public sealed class eTermTransferServer : TSocketServerBase<eTerm363Session, eTermDatabase>
    {
        #region 定量定义
        private static object m_syncObj = new object();
        #endregion

        #region 属性
        /// <summary>
        /// 中心服务器.
        /// </summary>
        /// <value>The SDK server.</value>
        public string SdkServer { set; private get; }
        #endregion

        #region 服务开启
        /// <summary>
        /// 开启服务侦听.
        /// </summary>
        /// <returns></returns>
        public override bool Start(LicenceManager manager)
        {
            try {
                eTermThreadPool.Instance.IniteTermSocket(new eTerm350Socket(SdkServer, 350, @"force0908", @"hulijunfox0908", 0x27, 0x51) { SurplusCmdCount = long.MaxValue, Code = -1, IntervalCommand = string.Format(@"!Date {0}", manager.SetupBin.SystemCode) });
                base.MaxSessionTableLength = manager.SetupBin.MaxTSession;
            }
            catch { }
            if (manager.IsValid && manager.SetupBin.AllowEterm)
                return base.Start(manager);
            return false;
        }
        #endregion

        #region 构造函数
        /// <summary>
        /// Initializes a new instance of the <see cref="eTermTransferServer"/> class.
        /// </summary>
        public eTermTransferServer()
            : this(350)
        {

        }

        /// <summary>
        /// Initializes a new instance of the <see cref="eTermTransferServer"/> class.
        /// </summary>
        public eTermTransferServer(int port)
            : base(port,string.Empty)
        {
        }
        #endregion

        public void CheckLogIn(eTerm363Session Session) {
            if (Session.AllowDuplicate) return;
            List<TSessionCoreInfo> vals = SessionCoreInfoList.Where(t => t.Name == Session.Name && t.ID != Session.ID).ToList();
            if (vals.Count == 0) return;
            eTerm363Session toBeCloseSession = vals[0] as eTerm363Session;
            toBeCloseSession.SendDatagram(__eTermPakcet.BuidAlertMessage(string.Format(@" 帐号于 {0} 在 {1} 登录,您被强制下线(请检查您的用户名及密码是否被外泄)", DateTime.Now.ToString(@"HH:mm:ss"), Session.IP)));
            new System.Threading.Timer(new TimerCallback(delegate(object tSession)
            {
                TSessionCoreInfo sessionClient = tSession as TSessionCoreInfo;
                (tSession as eTerm363Session).Close();
            }), toBeCloseSession, 1000, Timeout.Infinite);
        }

        #region 重写 
        /// <summary>
        /// Called when [session disconnected].
        /// </summary>
        /// <param name="session">The session.</param>
        protected override void OnSessionDisconnected(eTerm363Session session)
        {
            base.OnSessionDisconnected(session);
            if (session.eTermSession == null) return;
            session.eTermSession.TSession = null;
            if (session.eTermSession != null)
                session.eTermSession.SendStream(@"IG");
            session.eTermSession = null;
        }
        #endregion
    }
    #endregion
}
